package com.sjz.bookmark.tree;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.io.IOException;

public class TreeTransferHandler extends TransferHandler {

    private final JTree tree;

    public TreeTransferHandler(JTree tree) {
        this.tree = tree;
    }

    @Override
    public int getSourceActions(JComponent c) {
        return MOVE;
    }

    @Override
    protected Transferable createTransferable(JComponent c) {
        TreePath path = tree.getSelectionPath();
        if (path != null) {
            return new TreeTransferable(path);
        }
        return null;
    }

    @Override
    protected void exportDone(JComponent source, Transferable data, int action) {
        if (action == MOVE) {
            // 这里可以执行一些操作，比如更新数据模型
            System.out.println("Moved nodes");
        }
    }

    @Override
    public boolean canImport(TransferSupport support) {
        if (!support.isDrop()) {
            return false;
        }
        support.setShowDropLocation(true);
        return true;
    }

    @Override
    public boolean importData(TransferSupport support) {
        if (!canImport(support)) {
            return false;
        }

        try {
            // 获取拖拽的源节点路径
            TreePath path = (TreePath) support.getTransferable().getTransferData(TreeTransferable.TREE_PATH_FLAVOR);
            System.out.println("TreePath: " + path);

            DefaultMutableTreeNode sourceNode = (DefaultMutableTreeNode) path.getLastPathComponent();
            System.out.println("Source Node: " + sourceNode);
            System.out.println("Source Node UserObject: " + sourceNode.getUserObject());

            // 获取拖拽的目标节点路径
            JTree.DropLocation dl = (JTree.DropLocation) support.getDropLocation();
            TreePath dest = dl.getPath();
            DefaultMutableTreeNode destNode = (DefaultMutableTreeNode) dest.getLastPathComponent();

            System.out.println("Destination Node: " + destNode.getUserObject());

            // 如果目标节点是源节点的祖先节点
            if (destNode.isNodeAncestor(sourceNode)) {
                System.out.println("Moved to parent node: " + destNode);
            }

            // 更新树模型
            ((DefaultTreeModel) tree.getModel()).reload();

            return true;
        } catch (UnsupportedFlavorException | IOException e) {
            e.printStackTrace();
        }
        return false;
    }

    private static class TreeTransferable implements Transferable {

        public static final DataFlavor TREE_PATH_FLAVOR = new DataFlavor(TreePath.class, "Tree Path");

        private final TreePath path;

        public TreeTransferable(TreePath path) {
            this.path = path;
        }

        @Override
        public DataFlavor[] getTransferDataFlavors() {
            return new DataFlavor[]{TREE_PATH_FLAVOR};
        }

        @Override
        public boolean isDataFlavorSupported(DataFlavor flavor) {
            return flavor.equals(TREE_PATH_FLAVOR);
        }

        @Override
        public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException {
            if (isDataFlavorSupported(flavor)) {
                return path;
            }
            throw new UnsupportedFlavorException(flavor);
        }
    }
}